﻿#region Using namespaces

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Reflection;

using BOL.Apps.GeneNetworkBrowser;
using BOL.Apps.GeneNetworkBrowser.Controls;
using BOL.Apps.GeneNetworkBrowser.Converters;
using BOL.Apps.GeneNetworkBrowser.IO;

using GraphSharp.Controls;

#endregion

namespace BOL.Apps.GeneNetworkBrowser.ViewModels
{
    public sealed class SubNetworksDocumentViewModel : DocumentViewModel
    {
        #region Private variables

        private IEnumerable<Network> _subNetworks;

        #endregion

        #region Public properties

        public IEnumerable<Network> SubNetworks
        {
            get { return _subNetworks; }
            set
            {
                if (value != _subNetworks)
                {
                    _subNetworks = value;
                    IsChanged = true;
                    OnPropertyChanged("SubNetworks");
                }
            }
        }

        public ICommand SelectAllCheckedCommand { get; private set; }

        #endregion

        #region Constructor

        public SubNetworksDocumentViewModel(IEnumerable<Network> subnetworks, string fileName)
            : base(fileName)
        {
            SubNetworks = subnetworks;
        }

        #endregion

        #region Private method

        private void OnSelectAllCheckedCommand(object arg)
        {
            if (arg == null || !(arg is NameValueConverterResult))
                return;

            NameValueConverterResult prop = arg as NameValueConverterResult;

            ////Reflect on the model to find the property we want to update. 
            //PropertyInfo propInfo = Averagers.FirstOrDefault().GetType().GetProperty(prop.Name);
            //if (propInfo == null) return;

            ////Set the property on the Model 
            //foreach (var item in Averagers)
            //    propInfo.SetValue(item, prop.Value, null);
        }

        #endregion

        #region Public method

        public void ApplyCox()
        {
            var gvr = new GeneValueReader("C:\\Command_Console\\Data\\TCGA200\\GX\\Survival\\LEY83FEATUREGENESCORES.txt", false, 1, new char[1] { '\t' });
            var gvrd = gvr.Read();

            gvr.Close();

            foreach (var subnetwork in _subNetworks)
            {
                foreach (var gene in subnetwork.Vertices)
                {
                    if (gvrd.ContainsKey(gene.Id))
                        gene.Attributes.Add("Cox", gvrd[gene.Id][0]);
                        //gene.Attributes.Add(name, Math.Abs(gvrd[gene.Id][0]));
                    else
                        gene.Attributes.Add("Cox", 0);
                }
            }
        }

        #endregion
    }
}
